package cn.tedu.system.product.controller;

import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.product.pojo.dto.AgricultureProductParam;
import cn.tedu.system.product.pojo.dto.LiveProductParam;
import cn.tedu.system.product.pojo.vo.LiveProductVO;
import cn.tedu.system.product.pojo.vo.ProductVO;
import cn.tedu.system.product.service.ProductService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "09.商品模块")
@RestController
@RequestMapping("/api/v1/product/")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("用户查看上架商品")
    @ApiOperationSupport(order = 10)
    @GetMapping("allPutProduct")
    public JsonResult allPut(@ApiIgnore HttpSession session) {
        log.debug("用户查看商家商品信息");
        List<ProductVO> list = productService.selectAll(session);
        return JsonResult.ok(list);
    }

    @ApiOperation("查看商品详情")
    @ApiOperationSupport(order = 20)
    @GetMapping("detail")
    public JsonResult detail(Long id, @ApiIgnore HttpSession session) {
        log.debug("查看商品详情参数:{}",id);
        ProductVO productVO = productService.selDetail(id,session);
        log.debug("查看商品详情结果:{}",productVO);
        return JsonResult.ok(productVO);
    }

    //杨平
    @ApiOperation("搜索商品")
    @ApiOperationSupport(order = 30)
    @GetMapping("search")
    public JsonResult search(String name, @ApiIgnore HttpSession session) {
        log.debug("搜索商品参数:{}",name);
        List<ProductVO> list = productService.search(name,session);
        return JsonResult.ok(list);
    }



    @ApiOperation("商家发布直播商品")
    @ApiOperationSupport(order = 40)
    @PostMapping("addLiveProduct")
    public JsonResult addLiveProduct(@RequestBody LiveProductParam  liveProduct, @ApiIgnore HttpSession session) {
        log.debug("商家发布直播商品参数:{}",liveProduct);
        productService.addProduct(liveProduct,session);
        return JsonResult.ok();
    }

    @ApiOperation("查看直播商品")
    @ApiOperationSupport(order = 50)
    @GetMapping("liveProduct")
    public JsonResult liveProduct(Long liveId, @ApiIgnore HttpSession session) {
        log.debug("查看直播商品参数:{}",liveId);
        List<LiveProductVO> list = productService.selectLiveProduct(liveId,session);
        return JsonResult.ok(list);
    }

    @ApiOperation("下架直播商品")
    @ApiOperationSupport(order = 60)
    @PostMapping("delLiveProduct")
    public JsonResult delLiveProduct(Long id, @ApiIgnore HttpSession session) {
        log.debug("下架直播商品参数:{}",id);
        productService.delLiveProduct(id,session);
        return JsonResult.ok();
    }

    @ApiOperation("管理员发布助农商品")
    @ApiOperationSupport(order = 70)
    @PostMapping("addAgricultureProduct")
    public JsonResult addAgricultureProduct(@RequestBody AgricultureProductParam productParam, @ApiIgnore HttpSession session) {
        log.debug("管理员发布助农商品参数:{}",productParam);
        productService.addAgricultureProduct(productParam,session);
        return JsonResult.ok();
    }

    @ApiOperation("管理员删除助农商品")
    @ApiOperationSupport(order = 80)
    @PostMapping("delAgricultureProduct/{id}")
    public JsonResult delAgricultureProduct(@PathVariable("id") Long id, @ApiIgnore HttpSession session) {
        log.debug("管理员删除助农商品参数:{}",id);
        productService.delAgricultureProduct(id,session);
        return JsonResult.ok();
    }
}
